package com.leetcode.partition9;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.TreeMap;

/**
 * @author `RKC`
 * @date 2021/12/30 10:45
 */
public class LC846一手顺子 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        int[] hand = new int[s.length];
        for (int i = 0; i < s.length; i++) hand[i] = Integer.parseInt(s[i]);
        int groupSize = Integer.parseInt(reader.readLine());
        writer.write(isNStraightHand(hand, groupSize) + "\n");
        writer.flush();
    }

    public static boolean isNStraightHand(int[] hand, int groupSize) {
        int n = hand.length;
        if (n % groupSize != 0) return false;
        Map<Integer, Integer> map = new HashMap<>();
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for (int item : hand) {
            map.put(item, map.getOrDefault(item, 0) + 1);
            heap.add(item);
        }
        while (!heap.isEmpty()) {
            //每次尝试从当前最小的出发
            int curr = heap.poll();
            if (map.get(curr) == 0) continue;
            for (int i = 0; i < groupSize; i++) {
                int cnt = map.getOrDefault(curr + i, 0);
                //没有连续的或者连续的数量不够就说明不能凑够
                if (cnt == 0) return false;
                map.put(curr + i, cnt - 1);
            }
        }
        return true;
    }
}
